from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from django.utils import timezone
from rest_framework.views import APIView

from users.models import User


class UserDayActiveView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = timezone.now().replace(hour=0,minute=0,second=0, microsecond=0)
        count = User.objects.filter(last_login__gte=now_date).count()
        response_date = {
            'date': now_date.date(),
            'count': count
        }
        return Response(response_date)


class UserDayOrdersView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = timezone.now().replace(hour=0,minute=0,second=0, microsecond=0)
        count = User.objects.filter(orders__create_time__gte=now_date).distinct().count()
        response_date = {
            'date': now_date.date(),
            'count': count
        }
        return Response(response_date)

class UserMonthCountView(APIView):
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = timezone.now().replace(hour=0,minute=0,
                                          second=0, microsecond=0)
        begin_date = now_date - timezone.timedelta(days=29)
        month_li = []
        current_date = begin_date
        while current_date <= now_date:
            next_date = current_date + timezone.timedelta(days=1)
            count = User.objects.filter(date_joined__gte=current_date,
                                        date_joined__lt=next_date).count()
            month_li.append({
                'count': count,
                'date': current_date.date()
            })
            current_date += timezone.timedelta(days=1)
        return Response(month_li)
